//
//  CCRTInputeVC.m
//  iSeal5200
//
//  Created by He Yuan on 12-12-14.
//  Copyright (c) 2012年 http://www.heyuan110.com. All rights reserved.
//

#import "InputeVC.h"
#import <QuartzCore/QuartzCore.h>

@interface InputeVC ()<UITextViewDelegate>
@end

@implementation InputeVC

@synthesize txtView = _txtView;
@synthesize countLbl = _countLbl;
@synthesize toolView = _toolView;
@synthesize initString = _initString;
@synthesize maxCount;

-(void)dealloc
{
    [self removeNotifications];
    [[NSNotificationCenter defaultCenter] removeObserver:self];\
}

- (void)viewDidUnload
{
    
    self.countLbl = nil;
    
    self.toolView = nil;
    
    self.txtView = nil;
    
    [super viewDidUnload];

}

-(id)initWithInitString:(NSString *)string maxCount:(unsigned)mc delegate:(id)delegate
{
    self = [super init];
    
    if (self)
    {
        self.initString = string;
        
        self.universalDelegate = delegate;
        
        maxCount = mc;
    }
    return self;
}

-(void)loadView
{
    [super loadView];
    
    CGRect rect = self.view.frame;
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    _txtView = [[PlaceholderTextView alloc]initWithFrame:CGRectMake(10,10,300,0) drawoffsety:8.0];
    _txtView.backgroundColor = [UIColor whiteColor];
    _txtView.delegate = self;
    _txtView.layer.cornerRadius = 12.0f;
    _txtView.layer.masksToBounds = YES;
    _txtView.text = _initString;
    _txtView.font = FONT_NORMAL;
    [self.view addSubview:_txtView];
    
    _toolView = [[UIView alloc]initWithFrame:CGRectMake(0,156 ,rect.size.width, 44.0f)];
    _toolView.backgroundColor = [UIColor colorWithWhite:0.814 alpha:1.000];
    [self.view addSubview:_toolView];
    
    _countLbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 44)];
    [_countLbl setTextColor:[UIColor darkGrayColor]];
    [_countLbl setBackgroundColor:[UIColor clearColor]];
    _countLbl.font = FONT_NORMAL;
    [_countLbl setText:[NSString stringWithFormat:@"0/%d",maxCount]];
    [_toolView addSubview:_countLbl];
    
    [_txtView becomeFirstResponder];
    
    [self textInputChanged:nil];
    
    [self registerForKeyboardNotifications];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setTitle:HYLocalizedString(@"填写内容") showBackButton:YES];
}

- (void) keyboardWasShown:(NSNotification *) notif
{
    NSDictionary *info = [notif userInfo];
    NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey
                      ];
    CGSize keyboardSize = [value CGRectValue].size;
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.30];
    _toolView.frame=CGRectMake(_toolView.frame.origin.x,self.view.frame.size.height-keyboardSize.height+4.0-44,
                               _toolView.frame.size.width,
                               _toolView.frame.size.height);
    _txtView.frame = CGRectMake(10,10, _txtView.frame.size.width, _toolView.frame.origin.y - 20);
	[UIView commitAnimations];
}

- (void) keyboardWasHidden:(NSNotification *) notif
{
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.30];
    _txtView.frame = CGRectMake(10,10, _txtView.frame.size.width,156);
    _toolView.frame=CGRectMake(_toolView.frame.origin.x,_txtView.frame.origin.y+_txtView.frame.size.height+10,
                               _toolView.frame.size.width,
                               _toolView.frame.size.height);
	[UIView commitAnimations];
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    return YES;
}

- (void)textInputChanged:(NSNotification *)notif
{
    NSString *txtString = [MSTool trimmingWhiteSpaceCharacter:_txtView.text];
    NSInteger count = [txtString length];
    if (count > maxCount)
    {
        count = maxCount;
        _txtView.text = [txtString substringToIndex:count];
        [[HYProgressHUD sharedProgressHUD]setText:HYLocalizedString(@"亲，超过字数限制了!")];
        [[HYProgressHUD sharedProgressHUD]showInView:[[[UIApplication sharedApplication] windows] lastObject]];
        [[HYProgressHUD sharedProgressHUD]done:NO];
    }
    _countLbl.text = [NSString stringWithFormat:@"%d/%d",count,maxCount];
}


- (void) registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textInputChanged:)
                                                 name:UITextViewTextDidChangeNotification
                                               object:nil];
}

-(void)removeNotifications
{
	[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    
	[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

